import { ref, reactive } from 'vue'
export const authCode = ref<string>('') // 用户登录时验证验证码是否一致

// 手机号码自定义规则
const validatePhone = (_rule: any, value: any, callback: any) => {
  const phoneExp = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/
  if (phoneExp.test(value)) {
    callback()
  } else {
    // 手机号码不正确
    callback(new Error('请输入正确的手机号码！'))
  }
}
// 用户登录验证
// 验证码自定义规则
const validateAuthCode = (_rule: any, value: any, callback: any) => {
  if (value === authCode.value) {
    callback()
  } else {
    // 验证码不正确
    callback(new Error('请输入正确的验证码！'))
  }
}
//登录规则
export const loginRules = reactive({
  phone: [
    { required: true, message: '请输入手机号码！', trigger: 'blur' },
    { validator: validatePhone, trigger: 'blur' }, //自定义验证规则
  ],
  authCode: [
    { required: true, message: '验证码为空！', trigger: 'blur' },
    { validator: validateAuthCode, trigger: 'blur' }, //自定义验证规则
  ],
})
